package springboot.demo.shiro.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import springboot.demo.shiro.entity.po.User;
import springboot.demo.shiro.service.IUserService;

import java.util.List;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author liwq
 * @since 2018-11-20
 */
@RestController
@RequestMapping("/user")
public class UserController {

    private final IUserService userService;

    @Autowired
    public UserController(IUserService userService) {
        this.userService = userService;
    }

    @GetMapping("/getList")
    public List<User> getUserList() {
        IPage<User> userPage = userService.page(new Page<>(1, 10), new QueryWrapper<User>().
                eq("name", "liwq"));
        return userPage.getRecords();
    }

    @PostMapping("/add")
    public void addUser(@RequestBody User user) {
        userService.save(user);
    }

    @PostMapping("/addList")
    public void addUserList(@RequestBody List<User> userList) {
        userService.saveBatch(userList);
    }

    @PostMapping("/update")
    public void updateUser(@RequestBody User user) {
        userService.update(user, new UpdateWrapper<User>().eq("name", user.getName()));
    }

    @DeleteMapping("/remove")
    public void removeUser(@RequestParam String name) {
        userService.remove(new QueryWrapper<User>().eq("name", name));
    }
}
